import {ModelDesignApi} from "@/api/modeldesign";

/**
 * API 工具类
 * @type {{upScreenPreview(*, *): Promise<void>}}
 */
const ApiUtils = {
    /**
     * 上屏预览申请
     * @param _this
     * @param formConfig
     * @param widgetList
     * @returns {Promise<void>}
     */
    async upScreenPreview(_this, formConfig,widgetList,resolution) {
        let indexUrl = localStorage.getItem("previewJson_indexUrl");
        let terminalId = localStorage.getItem("previewJson_terminalId");
        let terminalType = localStorage.getItem("previewJson_terminalType");
        let baseURL = localStorage.getItem("previewJson_baseURL");
        if (indexUrl == null || indexUrl == '') {
            _this.$message.error("请设置indexUrl【previewJson_indexUrl】,格式为：http://你本地的IP:端口/");
            // 每个人本地可能不一样，自己设定自己的IP 方便调试
            // indexUrl = 'http://localhost:8081/#/preview';
            localStorage.setItem("previewJson_indexUrl", '');
            return
        }
        if (terminalId == null || terminalId == '') {
            localStorage.setItem("previewJson_terminalId", '');
            _this.$message.error("请设置终端ID【previewJson_terminalId】");
            return
        }
        if (terminalType == null || terminalType == '') {
            localStorage.setItem("previewJson_terminalType", '');
            _this.$message.error("请设置终端类型【previewJson_terminalType】");
            return
        }
        if (baseURL == null) {
            baseURL = '';// 可以指定路由前缀  例如设置  baseURL = '/206_clinic/',表示访问206 终端
            localStorage.setItem("previewJson_baseURL", baseURL);
        }
        // 组装预览JSON
        let previewJson = {
            terminalId: terminalId,
            terminalType: terminalType,
            formConfig: JSON.stringify(formConfig),
            widgetList: JSON.stringify(widgetList),
            templateUrl: "/#/preview",
            indexUrl: indexUrl,
            resolution: resolution,
            baseURL: baseURL
        }
        const {code, msg} = await ModelDesignApi.sendMsgToScreenLoadLocalTemplate(previewJson);
        _this.$message.success("已经发起上屏申请，请耐心等待！");
    }
}
export {ApiUtils}